<?php
namespace miniapp\cpmin\controller;

use BigFish\PDF417\PDF417;
use BigFish\PDF417\Renderers\ImageRenderer;
use miniapp\cpmin\service\CplistService;
use app\common\controller\MiniappAddon;
use miniapp\cpmin\service\HomeService;
use miniapp\cpmin\validate\ApiValidate;
use Picqer\Barcode\BarcodeGeneratorJPG;
use Ramsey\Uuid\Uuid;
use think\facade\Log;

class Api extends MiniappAddon{
    public $isCheckLogin = true;//开启登录验证

    public function initialize()
    {
        $rd_session = input('rd_session');//key可自定：可以使用token、session_id,我在此使用rd_session
        if ($rd_session) {
            //如果存在赋值 $this->rd_session
            $this->rd_session = $rd_session;
        }
        //要求放最后面
        parent::initialize();
    }
    //获取服务器访问票据
    public function index()
    {
        //返回小程序
        return json(['code'=>0,'msg'=>'访问票据获取成功','data'=>['rd_session' => $this->rd_session]]);
    }
    //扫码兑奖
    public function scan(){
        $code = input('code','','string');
        $param = [
            'code' => $code
        ];
        //验证参数
        $v = new ApiValidate();
        if(!$v->scene('scanCode')->check($param)){
            return json(['code'=>1,'msg'=>$v->getError(),'data'=>'']);
        }
        $pdf417 = new PDF417();
        $codeData = $pdf417->encode($code);
        $renderer = new ImageRenderer([
            'format' => 'png'
        ]);
        $barcodePng = $renderer->render($codeData);
        $uuid = Uuid::uuid1();
        $fileName = md5($uuid->toString()).'.jpg';
        $result = qiniuUploadBin($this->_mid,$barcodePng,$fileName);
        $img_url = $result['src'];
        $openid = $this->userinfo['openid'];
        $data = [
            'img_url'=>$img_url,
            'openid'=>$openid,
            'balance'=>0,
            'code' => $code
        ];
        $cplistS = new CplistService();
        $r = $cplistS->addCode($data);
        if($r){
            return json(['code'=>0,'msg'=>'添加成功','data'=>'']);
        }else{
            return json(['code'=>1,'msg'=>'添加失败','data'=>'']);
        }
    }
    //获取开奖结果
    public function lists(){
        $openid = $this->userinfo['openid'];
        $page = input('page',1,'int');
        $limit = input('limit',10,'int');
        $homeS = new HomeService();
        try{
            $data = $homeS->lists($openid,$page,$limit);
            return json(['code'=>0,'msg'=>'获取列表数据成功','data'=>$data]);
        }catch (\Exception $exception){
            return json(['code'=>1,'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }
    //保存用户名和手机号
    public function savent(){
        $openid = $this->userinfo['openid'];
        $name = input('name','','string');
        $tel = input('tel','','string');
        $data = [
            'mobile'=> $tel,
            'relname'=> $name,
            'openid' => $openid
        ];
        $cplistS = new CplistService();
        $r = $cplistS->saveNt($data);
        if($r){
            return json(['code'=>0,'msg'=>'保存成功','data'=>'']);
        }else{
            return json(['code'=>1,'msg'=>'保存失败','data'=>'']);
        }
    }
    //获取微信用户信息
    public function getuserinfo(){
        $id = $this->userinfo['id'];
        $cplistS = new CplistService();
        $gr = $cplistS->getNameTel($id);
        $this->userinfo['mobile'] = $gr['mobile'];
        $this->userinfo['relname'] = $gr['relname'];
        return json(['code'=>0,'msg'=>'获取用户信息成功','data'=>$this->userinfo]);
    }
    //encryptedData解密
    public function getEncryptedData(){
        $code = input('code');
        $encryptedData = input('encryptedData');
        $iv = input('iv');
        $rawData = input('rawData');
        $signature =input('signature');
        $miniprogram = getMiniProgramObj();
        $data = $miniprogram ->checkLogin($code, $encryptedData, $iv, $rawData, $signature);
        if($data!==false){
            return json(['code'=>0,'msg'=>'encryptedData解密成功','data'=>$data]);
        }else{
            return json(['code'=>0,'msg'=>'encryptedData解密失败','data'=>'']);
        }
    }
}
